package gui;

import java.io.File;

import gamemngr.GameMngr;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import resources.Configuration;

import com.swtdesigner.SWTResourceManager;

public class SaveDialog extends Dialog {

	private Text text;
	protected Object result;
	protected Shell shell;
	private GameMngr game;
	private SaveData saveData;

	/**
	 * Create the dialog
	 * @param parent
	 * @param style
	 */
	public SaveDialog(Shell parent, int style) {
		super(parent, style);
	}

	/**
	 * Create the dialog
	 * @param parent
	 * @param saveData 
	 */
	public SaveDialog(Shell parent, GameMngr game, SaveData saveData) {
		this(parent, SWT.NONE);
		this.game = game;
		this.saveData = saveData;
	}

	/**
	 * Open the dialog
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		return result;
	}

	/**
	 * Create contents of the dialog
	 */
	protected void createContents() {
		shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
		shell.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
		shell.setLocation(RiskGUI.getNewPosition(getParent()));
		shell.setSize(249, 131);
		shell.setText("Save");

		final Label chooseTheGameLabel = new Label(shell, SWT.NONE);
		chooseTheGameLabel.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
		chooseTheGameLabel.setFont(SWTResourceManager.getFont("Arial", 12, SWT.NONE));
		chooseTheGameLabel.setText("Choose the game name to save:");
		chooseTheGameLabel.setBounds(10, 10, 303, 20);

		text = new Text(shell, SWT.BORDER);
		text.setFont(SWTResourceManager.getFont("", 12, SWT.NONE));
		text.setBounds(10, 36, 223, 25);

		final Button saveButton = new Button(shell, SWT.NONE);
		saveButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				// preform save on the game mngr
				if(text.getText().trim().equals("")){
					AutoDialogMessegaes.displayErrorMsg("Error", "Wrong save name. Try again.", shell);
					return;
				}
				String saveName = Configuration.saveDir + File.separator + text.getText() + ".data";
				saveData.setSaveName(saveName);
				game.saveGame(new File(saveName));
				AutoDialogMessegaes.displayFeedbackMsg("Success", "Saving \"" + text.getText() + "\" was successful", shell);
				shell.dispose();
			}
		});
		saveButton.setText("save");
		saveButton.setBounds(143, 67, 42, 23);

		final Button cancelButton = new Button(shell, SWT.NONE);
		cancelButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				shell.dispose();
			}
		});
		cancelButton.setText("Cancel");
		cancelButton.setBounds(191, 67, 42, 23);
		//
	}
}
